/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package t7;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.net.ssl.HttpsURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 *
 * @author BEO
 */
public class GoogleDocs {
    private static String GoogleGet(String url) {
      String r="";
      HttpsURLConnection connection = null;
      OutputStreamWriter wr = null;
      BufferedReader rd  = null;
      StringBuilder sb = null;
      String line = null;
    
      URL serverAddress = null;
    
      try {
          serverAddress = new URL(url);
          //set up out communications stuff
          connection = null;
        
          //Set up the initial connection
          connection = (HttpsURLConnection)serverAddress.openConnection();
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.setReadTimeout(10000);
                    
          connection.connect();
        
          //get the output stream writer and write the output to the server
          //wr = new OutputStreamWriter(connection.getOutputStream());
          //wr.write("");
          //wr.flush();
        
          //read the result from the server
          rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          sb = new StringBuilder();
        
          while ((line = rd.readLine()) != null)
          {
                sb.append(line + '\n');
          }
        
          //System.out.println(sb.toString());
          String s= sb.toString();
          r=s.substring(0,s.length()-1);
                    
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();
          rd = null;
          sb = null;
          wr = null;
          connection = null;
      }        
        return r;
    }
    
    
    private static String[] GooglePost(String url,String param,String Cookie) {
        String[] result=new String[3];
        URL Url;
        HttpsURLConnection connection = null;  
        try {
          //Create connection
          Url = new URL(url);
          connection = (HttpsURLConnection)Url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
          connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
          connection.setRequestProperty("Accept-Language","en-us,en;q=0.5");
          connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
          connection.setRequestProperty("Content-Type", 
               "application/x-www-form-urlencoded");
         // connection.setRequestProperty("Referer", "https://accounts.google.com/ServiceLogin?hl=vi&continue=https://www.google.com/webhp?hl=vi&tab=mw");
          connection.setRequestProperty("Content-Length", "" + 
                   Integer.toString(param.getBytes().length));
          connection.setRequestProperty("Content-Language", "en-US");  
          connection.setRequestProperty("Cookie",Cookie);

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);
          //connection.setFollowRedirects(false); 

          //Send request
          DataOutputStream wr = new DataOutputStream ( connection.getOutputStream ());
          wr.writeBytes (param);
          wr.flush ();
          wr.close ();
          
          result[0]=Integer.toString(connection.getResponseCode());
          
          result[1]=connection.getHeaderField("Set-Cookie").split(";")[0];

          //Get Response	
          BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String line=rd.readLine();
          StringBuffer response = new StringBuffer(); 
          while((line = rd.readLine()) != null) {
            response.append(line+'\n');
          }
          rd.close();
          result[2]=response.toString();
          //System.out.println(line);     
         

      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
          finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }        
        return result;
    }
    
    private static void GoogleLogin() {
        String[] tmp,tmp2;
        tmp=GooglePost("https://accounts.google.com/ServiceLoginAuth","continue=https://www.google.com/webhp?hl=vi&tab=mw&dsh=7377188841314176938&hl=vi&GALX=2D_CSJvjTFc&pstMsg=1&dnConn=&checkConnection=youtube:1171:1&checkedDomains=youtube&timeStmp=&secTok=&Email=matran19900&Passwd=Phamthilien@%((!&signIn=Đăng nhập&PersistentCookie=yes&rmShown=1","continue=https%3A%2F%2Fwww.google.com%2Fwebhp%3Fhl%3Dvi%26tab%3Dmw&dsh=7377188841314176938&hl=vi&GALX=2D_CSJvjTFc&pstMsg=1&dnConn=&checkConnection=youtube%3A1171%3A1&checkedDomains=youtube&timeStmp=&secTok=&Email=matran19900&Passwd=Phamthilien%40%25%28%28%21&signIn=%C4%90%C4%83ng+nh%E1%BA%ADp&PersistentCookie=yes&rmShown=1");
        String cookie=tmp[1];
        System.out.println(cookie);
          if(!cookie.isEmpty()){
              tmp2=GooglePost("https://accounts.google.com/ServiceLoginAuth","continue=https://www.google.com/webhp?hl=vi&tab=mw&dsh=7377188841314176938&hl=vi&GALX=2D_CSJvjTFc&pstMsg=1&dnConn=&checkConnection=youtube:1171:1&checkedDomains=youtube&timeStmp=&secTok=&Email=matran19900&Passwd=Phamthilien@%((!&signIn=Đăng nhập&PersistentCookie=yes&rmShown=1",cookie);
              System.out.println(tmp2[1]);
          }

    }
    
    public static void maind(String args[]) {
        GoogleLogin();
    }
}
